package com.liuhuan.maildemo.controller;

import com.liuhuan.maildemo.util.CreateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class SendRegMail {
    @Autowired
    JavaMailSender javaMailSender;
     @Autowired
    RedisTemplate<String, String> redisTemplate;
    public void sendMail(String to,String subject,String text ) {
        //创建一封邮件
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        //设置收件人
        mailMessage.setTo(to);
        //设置发件人
        mailMessage.setFrom("1321261535@qq.com");
        //设置标题
        mailMessage.setSubject(subject);
        //设置正文
        mailMessage.setText(text);
        //发送邮件
        javaMailSender.send(mailMessage);
    }

    @RequestMapping("/send1")
    public String send1(String to) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        CreateCode createCode=new CreateCode();
        String code=createCode.randomCode();
        redisTemplate.opsForValue().set(to,code,5, TimeUnit.MINUTES);
        sendMail( to,"注册邮箱验证","[二手书城] 你正[在注册二手书城用户]，验证码"+code+"。验证码5分钟内有效，请勿告诉他人，如非本人操作，请忽略此消息。");
        return "成功";
    }

}
